<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output encoding="utf-8" method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="collection">
         <xsl:apply-templates select="collection"/>
      </xsl:when>
      <xsl:otherwise>
        Warning: The metadata of this object is not set properly. Or it might be missing or destoried.
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="collection">  
    <xsl:choose>
      <xsl:when test="marc:record">
         <xsl:apply-templates select="marc:record"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="marc:record">
    <xsl:choose>      
      <xsl:when test="marc:datafield[@tag='655' and @ind2='7']">
          <xsl:variable name="type" select="marc:datafield[@tag='655' and @ind2='7']/marc:subfield[@code='a']"/>
          <xsl:variable name="book">Book</xsl:variable>
          <xsl:variable name="book_chapter">Book Chapter</xsl:variable>
          <xsl:variable name="conference_paper">Conference Paper</xsl:variable>
          <xsl:variable name="conference_item">Conference Item</xsl:variable>
          <xsl:variable name="thesis">Thesis</xsl:variable>
          <xsl:variable name="journal_article">Journal Article</xsl:variable>
          <xsl:variable name="working_paper">Working Paper</xsl:variable>
          <xsl:variable name="reports">Report</xsl:variable>
          <xsl:variable name="other_resource">Other Resource</xsl:variable>
          <xsl:if test="$type = $book">
            <xsl:call-template name="printBook"/>
          </xsl:if>
          <xsl:if test="$type = $book_chapter">
            <xsl:call-template name="printBookChapter"/>
          </xsl:if>          
          <xsl:if test="$type = $conference_paper">
            <xsl:call-template name="printConferencePaper"/>
          </xsl:if>
          <xsl:if test="$type = $conference_item">
            <xsl:call-template name="printConferenceItem"/>
          </xsl:if>
          <xsl:if test="$type = $thesis">
            <xsl:call-template name="printThesis"/>
          </xsl:if>
          <xsl:if test="$type = $journal_article">
            <xsl:call-template name="printJournal"/>
          </xsl:if>
          <xsl:if test="$type = $working_paper">
            <xsl:call-template name="printWorkingPaper"/>
          </xsl:if>
          <xsl:if test="$type = $reports">
            <xsl:call-template name="printReports"/>
          </xsl:if>
          <xsl:if test="$type = $other_resource">
            <xsl:call-template name="printOtherResource"/>
          </xsl:if>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
    <!-- printOtherResource -->  
  <xsl:template name="printOtherResource">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- print result out -->
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/><xsl:value-of select="$title" />.
  </xsl:template>
  
  <!-- printBook -->  
  <xsl:template name="printBook">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- edition -->
    <xsl:variable name="edition">
      <xsl:if test="marc:datafield[@tag='250' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='250' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher -->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- print result out -->
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/><xsl:value-of select="$title" /><xsl:if test="string-length(normalize-space($edition)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$edition"/><xsl:value-of select="' '"/>edn</xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if>.
  </xsl:template>

    <!-- Book Chapter -->  
  <xsl:template name="printBookChapter">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>  
    <!-- Title of the Chapter -->
    <xsl:variable name="chapter">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Book Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='t']">
        <xsl:apply-templates select="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='t']"/>
      </xsl:if>
    </xsl:variable>
    <!-- edition -->
    <xsl:variable name="edition">
      <xsl:if test="marc:datafield[@tag='250' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='250' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher -->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Page Number -->
    <xsl:variable name="page">
      <xsl:if test="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='g']">
        <xsl:apply-templates select="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='g']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Editor -->
    <xsl:variable name="editor">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2=' ' and @tag='700']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2=' ' and @tag='700']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$chapter" />',<xsl:value-of select="' '"/><xsl:value-of select="$title" /><xsl:if test="string-length(normalize-space($editor)) &gt; 0">,<xsl:value-of select="' '"/>in<xsl:value-of select="' '"/><xsl:value-of select="$editor"/></xsl:if><xsl:if test="string-length(normalize-space($edition)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$edition"/><xsl:value-of select="' '"/>edn</xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if><xsl:if test="string-length(normalize-space($page)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$page"/></xsl:if>.
  </xsl:template>
  
  <!-- Conference Paper -->  
  <xsl:template name="printConferencePaper">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title of published proceeding  -->
    <xsl:variable name="otherTitle">
      <xsl:if test="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='t']">
        <xsl:apply-templates select="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']//marc:subfield[@code='t']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher -->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Page Information -->
    <xsl:variable name="page">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='g']">
        <xsl:apply-templates select="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='g']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Conference Information -->
    <xsl:variable name="confName">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="confPlace">  
      <xsl:if test="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="string-length(normalize-space($year)) &gt; 0">
        <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($otherTitle)) &gt; 0">,<xsl:value-of select="' '"/>In<xsl:value-of select="' '"/><xsl:value-of select="$otherTitle"/></xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if><xsl:if test="string-length(normalize-space($page)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$page"/></xsl:if>.
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="confdate" select="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='d']"/>
        <xsl:variable name="delimiter" select="' '"/>
        <xsl:variable name="confyear">
          <xsl:choose>
            <xsl:when test="contains($confdate, $delimiter)">
              <xsl:value-of select="substring-after($confdate, $delimiter)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$confdate"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="confmonth">
          <xsl:choose>
            <xsl:when test="contains($confdate, $delimiter)">
              <xsl:value-of select="substring-before($confdate, $delimiter)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="''"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$confyear" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($confName)) &gt; 0">,<xsl:value-of select="' '"/>Paper<xsl:value-of select="' '"/>presented<xsl:value-of select="' '"/>to<xsl:value-of select="' '"/><xsl:value-of select="$confName"/></xsl:if><xsl:if test="string-length(normalize-space($confPlace)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$confPlace"/></xsl:if><xsl:if test="string-length(normalize-space($confmonth)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$confmonth"/></xsl:if>.
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- Conference Item -->  
  <xsl:template name="printConferenceItem">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Conferenc Information -->
    <xsl:variable name="confName">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="confPlace">  
      <xsl:if test="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="confDate">
      <xsl:if test="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='d']">
        <xsl:apply-templates select="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']/marc:subfield[@code='d']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher -->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Page Information -->
    <xsl:variable name="page">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='g']">
        <xsl:apply-templates select="marc:datafield[@tag='787' and @ind1=' ' and @ind2='0']/marc:subfield[@code='g']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="string-length(normalize-space($year)) &gt; 0">
        <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if><xsl:if test="string-length(normalize-space($page)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$page"/></xsl:if>.
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="delimiter" select="' '"/>
        <xsl:variable name="confyear">
          <xsl:choose>
            <xsl:when test="contains($confDate, $delimiter)">
              <xsl:value-of select="substring-after($confDate, $delimiter)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$confDate"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="confmonth">
          <xsl:choose>
            <xsl:when test="contains($confDate, $delimiter)">
              <xsl:value-of select="substring-before($confDate, $delimiter)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="''"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$confyear" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($confName)) &gt; 0">,<xsl:value-of select="' '"/>Presented<xsl:value-of select="' '"/>to<xsl:value-of select="' '"/><xsl:value-of select="$confName"/></xsl:if><xsl:if test="string-length(normalize-space($confPlace)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$confPlace"/></xsl:if><xsl:if test="string-length(normalize-space($confmonth)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$confmonth"/></xsl:if>.
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- Thesis -->  
  <xsl:template name="printThesis">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>    
    <!-- Award -->
    <xsl:variable name="awardGiven">
      <xsl:if test="marc:datafield[@tag='502' and @ind1='3' and @ind2=' ']/marc:subfield[@code='u']">
        <xsl:apply-templates select="marc:datafield[@tag='502' and @ind1='3' and @ind2=' ']/marc:subfield[@code='u']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Institution -->
    <xsl:variable name="place">
      <xsl:value-of select="'University of New South Wales, Sydney'"/>
      <!-- no longer use this since they replace this with UNSW.
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:call-template name="getThesisInst">
          <xsl:with-param name="arg0" select="marc:datafield[@tag='100']"/>
        </xsl:call-template>
      </xsl:if>
      -->
    </xsl:variable>
    <xsl:variable name="award">
      <xsl:call-template name="replace">
        <xsl:with-param name="input" select="$awardGiven"/>
        <xsl:with-param name="lookfor" select="'Doctorate'"/>
        <xsl:with-param name="replacement" select="'Thesis'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($award)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$award"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if>.
  </xsl:template>

  <!-- Journal -->
  <xsl:template name="printJournal">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Journal Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Journal Name -->
    <xsl:variable name="name">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='t']">
        <xsl:apply-templates select="marc:datafield[@tag='787' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='t']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Volume Number --> <!-- Issue Number --> <!-- Page Number -->
    <xsl:variable name="other">
      <xsl:if test="marc:datafield[@tag='787' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='g']">
        <xsl:call-template name="volumeIssuePage">
          <xsl:with-param name="arg0" select="marc:datafield[@tag='787' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='g']"/>
        </xsl:call-template>  
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/>'<xsl:value-of select="$title"/>'<xsl:if test="string-length(normalize-space($name)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$name" /></xsl:if><xsl:value-of select="$other" />.
  </xsl:template>
  
  <!-- Working Paper -->  
  <xsl:template name="printWorkingPaper">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- SERIES -->
    <xsl:variable name="series">
      <xsl:if test="marc:datafield[@ind1=' ' and @ind2=' ' and @tag='830']">
        <xsl:apply-templates select="marc:datafield[@ind1=' ' and @ind2=' ' and @tag='830']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher  this is different from other types, they have publisher as b, place as a.-->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/><xsl:value-of select="$title"/><xsl:if test="string-length(normalize-space($series)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$series"/></xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if>.
  </xsl:template>
  
  <!-- Reports -->
  <xsl:template name="printReports">
    <!-- Author -->
    <xsl:variable name="author">
      <xsl:if test="marc:datafield[@tag='100']">
        <xsl:apply-templates select="marc:datafield[@tag='100']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Title -->
    <xsl:variable name="title">
      <xsl:if test="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
        <xsl:apply-templates select="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Series No. -->
    <xsl:variable name="series">
      <xsl:if test="marc:datafield[@ind1=' ' and @ind2=' ' and @tag='830']">
        <xsl:apply-templates select="marc:datafield[@ind1=' ' and @ind2=' ' and @tag='830']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publisher -->
    <xsl:variable name="publisher">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']"/>
      </xsl:if>
    </xsl:variable>
    <!-- Publication Place -->
    <xsl:variable name="place">
      <xsl:if test="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
        <xsl:apply-templates select="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']"/>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$author" /><xsl:value-of select="' '"/><xsl:value-of select="$year" />,<xsl:value-of select="' '"/><xsl:value-of select="$title"/><xsl:if test="string-length(normalize-space($series)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$series"/></xsl:if><xsl:if test="string-length(normalize-space($publisher)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$publisher"/></xsl:if><xsl:if test="string-length(normalize-space($place)) &gt; 0">,<xsl:value-of select="' '"/><xsl:value-of select="$place"/></xsl:if>.
  </xsl:template>
  
  <!-- Author Information -->
  <xsl:template match="marc:datafield[@tag='100']">
    <xsl:variable name="pointer" select="position()" />
    <xsl:variable name="total" select="last()" />
    <xsl:choose>
      <xsl:when test="subfield[@code='a']">
        <xsl:call-template name="getAuthor">
          <xsl:with-param name="arg0" select="subfield[@code='a']"/>
        </xsl:call-template>
        <xsl:if test="$pointer = $total"><xsl:value-of select="' '"/></xsl:if>
        <xsl:if test="$pointer = $total - 1"><xsl:value-of select="' '"/>&amp;<xsl:value-of select="' '"/></xsl:if>
        <xsl:if test="$pointer &lt; $total - 1">,<xsl:value-of select="' '"/></xsl:if>
      </xsl:when>
      <xsl:when test="marc:subfield[@code='a']">
        <xsl:call-template name="getAuthor">
          <xsl:with-param name="arg0" select="marc:subfield[@code='a']"/>
        </xsl:call-template>
        <xsl:if test="$pointer = $total"><xsl:value-of select="' '"/></xsl:if>
        <xsl:if test="$pointer = $total - 1"><xsl:value-of select="' '"/>&amp;<xsl:value-of select="' '"/></xsl:if>
        <xsl:if test="$pointer &lt; $total - 1">,<xsl:value-of select="' '"/></xsl:if>
      </xsl:when>
    </xsl:choose>  
  </xsl:template>
  
  <!-- Editor -->
  <xsl:template match="marc:datafield[@ind1='1' and @ind2=' ' and @tag='700']">
    <xsl:variable name="pointer" select="position()" />
    <xsl:variable name="total" select="last()" />
    <xsl:variable name="editorC">Editor</xsl:variable>
    <xsl:variable name="delimiter" select="', '" />
    <xsl:variable name="end">
      <xsl:if test="$total > 1"><xsl:value-of select="' '"/>(eds)</xsl:if>
      <xsl:if test="$total = 1"><xsl:value-of select="' '"/>(ed.)</xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="marc:subfield[@code='e']">
        <xsl:variable name="editorV" select="marc:subfield[@code='e']"/>
        <xsl:if test="$editorV = $editorC">
          <xsl:if test="marc:subfield[@code='a']">
            <xsl:call-template name="editorName">
              <xsl:with-param name="arg0" select="marc:subfield[@code='a']"/>
            </xsl:call-template>
            <xsl:if test="$pointer &lt; $total"><xsl:value-of select="$delimiter"/></xsl:if>
            <xsl:if test="$pointer = $total"><xsl:value-of select="$end"/></xsl:if>
          </xsl:if>
        </xsl:if>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <!-- Year -->  
  <xsl:template match="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='c']">
    <xsl:value-of select="."/>
  </xsl:template>
  
  <!-- Title -->
  <xsl:template match="marc:datafield[@ind1='1' and @ind2='0' and @tag='245']">
    <xsl:choose>
      <xsl:when test="marc:subfield[@code='a']"><xsl:value-of select="marc:subfield[@code='a']"/></xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <!-- Top Title -->
  <xsl:template match="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='t']">
    <xsl:value-of select="."/>
  </xsl:template>

  <!-- Book Page Number -->
  <xsl:template match="marc:datafield[@ind1=' ' and @ind2='0' and @tag='787']/marc:subfield[@code='g']">
    <xsl:call-template name="replace">
      <xsl:with-param name="input" select="."/>
      <xsl:with-param name="lookfor" select="'.'"/>
      <xsl:with-param name="replacement" select="'. '"/>
    </xsl:call-template>
  </xsl:template>
  
  <!-- Publisher -->
  <xsl:template match="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='b']">
    <xsl:value-of select="."/>
  </xsl:template>  

  <!-- Publication Place -->
  <xsl:template match="marc:datafield[@tag='260' and @ind1=' ' and @ind2=' ']/marc:subfield[@code='a']">
    <xsl:value-of select="."/>
  </xsl:template>
  
  <!-- Conference Publication Information -->
  <xsl:template match="marc:datafield[@ind1='0' and @ind2=' ' and @tag='711']">
    <xsl:if test="marc:subfield[@code='t']">,<xsl:value-of select="' '"/><xsl:value-of select="marc:subfield[@code='t']" /></xsl:if><xsl:if test="marc:subfield[@code='c']">,<xsl:value-of select="' '"/><xsl:value-of select="marc:subfield[@code='c']" /></xsl:if><xsl:if test="marc:subfield[@code='d']">,<xsl:value-of select="' '"/><xsl:value-of select="marc:subfield[@code='d']" /></xsl:if>
  </xsl:template>
  
  <!-- Series No. -->
  <xsl:template match="marc:datafield[@ind1=' ' and @ind2=' ' and @tag='830']">
    <xsl:choose><xsl:when test="marc:subfield[@code='v']"><xsl:value-of select="."/></xsl:when></xsl:choose>
  </xsl:template>
  
  <!-- xsl functions -->
  <xsl:template name='getThesisInst'>
    <xsl:param name="arg0"/>
    <xsl:variable name="delimiter" select="', '" />
    <xsl:variable name="afterString" select="substring-after($arg0, $delimiter)" />
    <xsl:value-of select="substring-after($afterString, $delimiter)"/>
  </xsl:template>
  
  <xsl:template name='getAuthor'>
    <xsl:param name="arg0"/>
    <xsl:variable name="delimiter" select="', '" />
    <xsl:variable name="beforeString" select="substring-before($arg0, $delimiter)" />
    <xsl:variable name="afterString" select="substring-after($arg0, $delimiter)" />
    <xsl:choose>
      <xsl:when test="contains($afterString, $delimiter)">
        <xsl:call-template name="authorName">
          <xsl:with-param name="arg0" select="normalize-space($arg0)"/>
          <xsl:with-param name="afterString" select="substring-before($afterString, $delimiter)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="authorName">
          <xsl:with-param name="arg0" select="normalize-space($arg0)"/>
          <xsl:with-param name="afterString" select="$afterString"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name='editorName'>
    <xsl:param name="arg0"/>
    <xsl:variable name="delimiter" select="', '" />
    <xsl:variable name="lastname">
      <xsl:choose>
        <xsl:when test="contains($arg0, $delimiter)"><xsl:value-of select="substring-before($arg0, $delimiter)"/></xsl:when>
        <xsl:otherwise><xsl:value-of select="$arg0"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="otherNames">
			<xsl:value-of select="translate(substring(substring-after($arg0, $delimiter), 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
		</xsl:variable>
    <xsl:value-of select="$otherNames"/><xsl:value-of select="' '" /><xsl:value-of select="$lastname"/>
  </xsl:template>
  
  <xsl:template name='authorName'>
    <xsl:param name="arg0"/>
    <xsl:param name="afterString"/>
    <xsl:variable name="delimiter" select="', '" />
    <xsl:variable name="beforeString" select="substring-before($arg0, $delimiter)" />
    <xsl:variable name="delimiterS" select="' '" />
    <xsl:choose>
      <xsl:when test="string-length(normalize-space($afterString)) = 0">        
        <xsl:value-of select="$beforeString" />
      </xsl:when>
      <xsl:when test="contains($afterString, $delimiterS)">
        <xsl:variable name="otherNames">
			<xsl:call-template name="getInitials">
				<xsl:with-param name="arg0" select="$afterString" />
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select="$beforeString" />, <xsl:value-of select="$otherNames" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="middleName">
          <xsl:call-template name="isInitial">          
            <xsl:with-param name="arg0" select="$afterString" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:if test="$middleName = 'true'"><xsl:value-of select="$beforeString" />, <xsl:value-of select="translate($afterString, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /></xsl:if>
        <xsl:if test="$middleName = 'false'"><xsl:value-of select="$beforeString" />, <xsl:value-of select="translate(substring($afterString, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /></xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="getInitials">
    <xsl:param name="arg0"/>
    <xsl:variable name="delimiter" select="' '" />
    <xsl:variable name="first" select="substring-before($arg0,$delimiter)" />
    <xsl:variable name="middle" select="substring-after($arg0,$delimiter)" />
    <xsl:value-of select="translate(substring($first, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    <xsl:value-of select="translate(substring($middle, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  </xsl:template>
  
  <xsl:template name="toUpper">
    <xsl:param name="arg0"/>
    <xsl:variable name="lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:value-of select="translate($arg0, $lower, $upper)" />
  </xsl:template>
  
  <xsl:template name='isUpper'>
    <xsl:param name="arg0"/>
    <xsl:variable name="lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:value-of select="contains($upper, $arg0)"/>
  </xsl:template>  
  
  <xsl:template name='isInitial'>
    <xsl:param name="arg0"/>
    <xsl:variable name="limit">2</xsl:variable>    
    <xsl:choose>
      <xsl:when test="string-length($arg0) = $limit">
        <xsl:call-template name="isUpper">
          <xsl:with-param name="arg0" select="substring($arg0, 2, 1)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="false()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="volumeIssuePage">
    <xsl:param name="arg0"/>
    <xsl:variable name="delimiter" select="';'" />
    <xsl:variable name="colon" select="':'" />
    <xsl:variable name="dot" select="'.'" />
    <xsl:variable name="volText">Vol:</xsl:variable>
    <xsl:variable name="issText">Issue:</xsl:variable>
	<xsl:variable name="ppText">pp.</xsl:variable>
    <xsl:variable name="volume">vol.<xsl:value-of select="' '"/></xsl:variable>
    <xsl:variable name="issue">no.<xsl:value-of select="' '"/></xsl:variable>
	<xsl:variable name="page">pp.<xsl:value-of select="' '"/></xsl:variable>
    <xsl:if test="contains($arg0, $volText)" >,<xsl:value-of select="' '"/><xsl:value-of select="$volume" />
		<xsl:choose>
		<xsl:when test="contains(substring-after($arg0, $volText), $delimiter)"><xsl:value-of select="substring-before(substring-after($arg0, $volText), $delimiter)" /></xsl:when>
		<xsl:otherwise><xsl:value-of select="substring-after($arg0, $volText)" /></xsl:otherwise>
		</xsl:choose>
	</xsl:if>
    <xsl:if test="contains($arg0, $issText)" >,<xsl:value-of select="' '"/><xsl:value-of select="$issue" />
		<xsl:choose>
		<xsl:when test="contains(substring-after($arg0, $issText), $delimiter)"><xsl:value-of select="substring-before(substring-after($arg0, $issText), $delimiter)" /></xsl:when>
		<xsl:otherwise><xsl:value-of select="substring-after($arg0, $issText)" /></xsl:otherwise>
		</xsl:choose>
	</xsl:if>
    <xsl:if test="contains($arg0, $ppText)" >,<xsl:value-of select="' '"/><xsl:value-of select="$page" /><xsl:value-of select="substring-after($arg0, $ppText)" /></xsl:if>
  </xsl:template>
  
  <xsl:template name="replace">
    <xsl:param name="input"/>
    <xsl:param name="lookfor"/>
    <xsl:param name="replacement"/>
    <xsl:choose>
      <xsl:when test="contains($input, $lookfor)">
        <xsl:value-of select="concat(substring-before($input, $lookfor), $replacement)"/>
        <xsl:call-template name="replace">
          <xsl:with-param name="input" select="substring-after($input, $lookfor)"/>
          <xsl:with-param name="lookfor" select="$lookfor"/>
          <xsl:with-param name="replacement" select="$replacement"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$input"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>